<template>
	<view class="page-course-detail">
		<view class="course-banner" :style="{backgroundImage: `url(${image})`}"></view>
		<view class="course-info">
			<text class="course-price">￥{{course.price}}</text>
			<view class="course-name-and-sold">
				<text class="course-name">{{course.name}}</text>
				<!-- <text class="course-sold">已售999</text> -->
			</view>
			<MxCourseDetailBlock class="cmp-course-detail-block" title="课时">
				<view class="block-info course-length">{{course.lessons}}节课</view>
			</MxCourseDetailBlock>
			
			<MxCourseDetailBlock class="cmp-course-detail-block" title="课时长">
				<view class="block-info class-hour">{{course.class_hour}}分钟</view>
			</MxCourseDetailBlock>
			
			<MxCourseDetailBlock class="cmp-course-detail-block" title="开班人数">
				<view class="block-info course-number">{{course.class_size}}人</view>
			</MxCourseDetailBlock>
			
			<MxCourseDetailBlock class="cmp-course-detail-block" title="开班时间">
				<view class="block-info course-time">
					<text class="course-time-item">{{course.opening_date}}</text> 
				</view>
			</MxCourseDetailBlock>
			
			<MxCourseDetailBlock class="cmp-course-detail-block" title="课程详情">
				<view class="block-info course-detail">
					<text class="course-time-item">{{course.course_detail}}</text>
				</view>
			</MxCourseDetailBlock>
			
			<view class="course-coach">
				<view class="course-coach-title">教练</view>
				<template v-for="coach in course.coachs">
					<view class="course-coach-card">
						<!-- <text class="course-coach-time">{{course.class_time}}</text> -->
						<view class="course-coach-avatar" :style="{backgroundImage: `url(${getImgUrl(coach)})`}"></view>
						<view class="course-coach-name">{{coach.name}}</view>
					</view>
					<view v-if="coach.graduated" class="coach-info graduated-school">
						<text>毕业院校：</text>
						<text>{{coach.graduated}}</text>
					</view>
					<view class="coach-info specialty">
						<text>教学特点：</text>
						<text>{{coach.specialty}}</text>
					</view>
					<view class="coach-info brief">
						<text>教练简介：</text>
						<text>{{coach.brief}}</text>
					</view>
				</template>
			</view>
			
			<MxBuyButton class="cmp-buy-button" :total="total" @buy="onBuy" />
		</view>
	</view>
</template>

<script>
	import {imgUrl} from '@/utils/config.js'
	import MxCourseDetailBlock from '@/components/course/MxCourseDetailBlock.vue';
	import MxBuyButton from '@/components/common/MxBuyButton.vue';
	import * as courseApi from '@/api/course.js'
	import { wxPayment } from '@/utils/app'
	
	export default {
		components: {
			MxCourseDetailBlock,
			MxBuyButton
		},
		data() {
			return {
				course:[],
				imgPrefix:imgUrl
			};
		},
		computed: {
			image() {
				return imgUrl + this.course.image?.file_path
			},
			
			// 课程价格
			total() {
				return Number(this.course.price)
			}
		},
		onLoad(option) {
			courseApi.detail({
				courseId: option.courseId
			}).then(res => {
				this.course = res.data
			})
		},
		
		methods: {
			onBuy(total) {
				console.log(total)
				courseApi.buy({courseId: this.course.course_id}).then(res => {
					wxPayment(res.data)
					  .then(() => {
						  this.$success('购买成功', function() {
							  setTimeout(() => {
								  uni.navigateTo({
								  	url: "/pages/mine/member-course"
								  })
							  }, 1500)
						  })
					  })
					  .catch(err => this.$error('支付取消'))
					  .finally(() => {
					    
					  })
				})
			},
			
			getImgUrl(coach) {
				return imgUrl + coach.image?.file_path
			}
		}
	}
</script>

<style scoped lang="scss">
@import  '@/common/scss/mixins.scss';
@import  '@/common/scss/var.scss';
page{
	padding-bottom: 64rpx;
}
.page-course-detail{
	.course-banner{
		width: 100%;
		padding-top: 100%;
		@include bg-img-cover;
	}
	
	.course-info{
		border-top-left-radius: 20px;
		border-top-right-radius: 20px;
		background-color: #fff;
		position: relative;
		top: -94rpx;
		box-sizing: border-box;
		padding: 72rpx 34rpx;
		font-family: $font-family;
		.course-price{
			font-size: 18px;
			font-weight: bold;
			color: $color-amount;
		}
		
		.course-name-and-sold{
			margin-top: 20rpx;
			display: flex;
			align-items: flex-end;
			justify-content: space-between;
			.course-name{
				font-size: 18px;
				color: $color-title;
			}
			
			.course-sold{
				color: #5E657D;
				font-size: 14px;
			}
		}
		
		.cmp-course-detail-block{
			display: flex;
			flex-direction: column;
			margin-top: 60rpx;
			.block-info{
				&.course-time{
					color:$color-info;
					.course-time-item{
						display: flex;
						margin-bottom: 18rpx;
						&:last-child{
							margin-bottom: 0;
						}
					}
				}
			}
		}
		
		.course-coach{
			margin-top: 84rpx;
			display: flex;
			flex-direction: column;
			.course-coach-title{
				align-self: center;
				display: inline-flex;
				justify-content: center;
				color: $color-title;
				width: 190rpx;
				text-align: center;
				font-size: 18px;
				font-weight: bold;
				position: relative;
				&::after{
					content: '';
					width: 190rpx;
					height: 10rpx;
					background-color: #3250FA;
					border-radius: 10rpx;
					position: absolute;
					left: 50%;
					bottom: -10rpx - 16rpx;
					transform: translateX(-50%);
				}
			}
			
			.course-coach-card{
				align-self: center;
				margin-top: 74rpx;
				margin-bottom: 60rpx;
				width: 416rpx;
				height: 540rpx;
				border-radius: 8px;
				background-image: $bg-gradient;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				color: #fff;
				.course-coach-time{
					font-size: 22px;
				}
				
				.course-coach-avatar{
					margin: 36rpx 0;
					width: 192rpx;
					height: 192rpx;
					border-radius: 50%;
					@include bg-img-cover;
				}
				
				.course-coach-name{
					font-size: 27px;
					font-weight: bold;
					text-align: center;
					padding: 0 40rpx;
				}
			}
			
			.coach-info{
				margin-bottom: 30rpx;
				&>text:first-child{
					color: $color-info;
				}
				
				&>text:last-child{
					color: $color-primary;
				}
				
				&.brief{
					&>text:last-child{
						display: block;
						margin-top: 20rpx;
						text-indent: 2em;
						text-align: justify;
					}
				}
			}
		}
	}
	
	.cmp-buy-button{
		margin-top: 80rpx;
		position: relative;
		left: 50%;
		transform: translateX(-50%);
	}
}
</style>
